<template>
	<view class="container">
		<view class="location-box" @click="handleMapSelection">
			<van-icon name="location" />
			<template v-if="location">
				<text>{{ location.address }}</text>
			</template>
			<template v-else>
				<text>暂未获得地理位置</text>
			</template>
			<text>点击切换</text>
		</view>
		<uni-popup ref="signUpDialog" type="dialog">
			<uni-popup-dialog cancelText="我再看看" confirmText="好的" title="提示" content="地理位置数据为空,前往授权？"
				@confirm="handleMapSelection"></uni-popup-dialog>
		</uni-popup>
		<view class="tag-box">
			<view class="tag-list">
				<view class="tag-item" @click="goTagJob(item)" :style="{ background: item.color }"
					v-for="(item, index) in tags" :key="index">{{ item.name }}</view>
			</view>
		</view>
		<view class="search-box">
			<uni-easyinput suffixIcon="search" clearable v-model="searchName" placeholder="搜索职位"
				@iconClick="searchClickHandler" @confirm="searchClickHandler"></uni-easyinput>
		</view>
		<view class="dropdown-box">
			<HM-filterDropdown :menuTop="menuTop" :position="menuPosition" :filterData="filterData"
				:defaultSelected="defaultSelected" :updateMenuName="true" @confirm="confirm"></HM-filterDropdown>
		</view>
		<view class="job-box">
			<view class="job-list" v-if="jobList.length">
				<view class="job-item" v-for="(item, index) in jobList" :key="index" @click="goJobDetail(item)">
					<view class="top">
						<view class="title">
							<text>{{ item.name }}</text>
							<text class="distance">距你{{ item.distance }}</text>
						</view>
						<view class="tag" v-if="item.taglist.length">
							<view class="tag-item" v-for="(tag_item, tag_index) in item.taglist" :key="tag_index">
								{{ tag_item }}</view>
						</view>
					</view>
					<view class="bottom">
						<text class="price">{{ item.salary }}</text>
						<view class="look-btn">去看看</view>
					</view>
				</view>
			</view>
			<view v-else><van-empty description="暂无职位" /></view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		getTopTag,
		getIndexTag,
		getAllJobNames,
		getAllJobs
	} from '@/api/public.js'

	export default {
		data() {
			return {
				tags: [],
				filterValue: Array(2),
				initialMenuTop: 0,
				menuTop: 0,
				menuPosition: 'absolute',
				defaultSelected: [],
				searchName: '',
				filterData: [{
						name: '全部职位',
						type: 'filter',
						submenu: [{
							name: '',
							submenu: []
						}]
					},
					{
						name: '标签选择',
						type: 'filter',
						submenu: [{
							name: '',
							submenu: []
						}]
					}
				],
				jobList: [],
				page: 1,
				pagesize: 10
			}
		},
		computed: {
			...mapState(['location'])
		},
		onLoad(options) {
			// 邀请码
			let {
				scene
			} = options
			if (scene) {
				uni.setStorageSync('invite_code', decodeURIComponent(options.scene))
			}
			// 计算fiexed块的top高度
			this.$nextTick(() => {
				const query = uni.createSelectorQuery().in(this)
				query
					.select('.dropdown-box')
					.boundingClientRect(data => {
						this.menuTop = data.top
						this.initialMenuTop = data.top
					})
					.exec()
				if (!this.location) {
					this.$refs['signUpDialog'].open()
				}
			})
			// 获得置顶标签
			getTopTag().then(res => {
				this.tags = res
			})
			// 获得筛选条件，全部职位和标签
			getAllJobNames().then(res => {
				this.filterData[0].submenu[0].submenu = res.map(item => {
					return {
						name: item.name,
						value: item.id
					}
				})
			})
			getIndexTag().then(res => {
				this.filterData[1].submenu[0].submenu = res.map(item => {
					return {
						name: item.name,
						value: item.id
					}
				})
			})
			// 加载职位列表
			this.loadData()
		},
		methods: {
			handleMapSelection() {
				let T = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.chooseLocation({
							latitude: res.latitude,
							longitude: res.longitude,
							success: function(res) {
								if (res.name) {
									T.jobList = []
									T.page = 1
									T.$store.dispatch('updateLocation', res)
									T.loadData()
								}
								// console.log('位置名称：' + res.name);
								// console.log('详细地址：' + res.address);
								// console.log('纬度：' + res.latitude);
								// console.log('经度：' + res.longitude);
							},
							fail: function(err) {
								console.log(err)
							}
						})
					},
					fail(e) {
						uni.showToast({
							title: '授权地理位置失败！',
							icon: 'none'
						})
					}
				})
			},
			searchClickHandler() {
				this.jobList = []
				this.page = 1
				this.loadData()
			},
			//接收菜单结果
			confirm(e) {
				const [f1, f2] = e.value
				this.filterValue[0] = f1[0]
				this.filterValue[1] = f2[0]
				this.jobList = []
				this.page = 1
				this.loadData()
			},
			goJobDetail(item) {
				uni.navigateTo({
					url: `./job/detail?id=${item.id}`
				})
			},
			goTagJob(item) {
				uni.navigateTo({
					url: `./job/tag?id=${item.id}&name=${item.name}`
				})
			},
			loadData() {
				let params = {
					page: this.page,
					pagesize: this.pagesize
				}
				if (this.filterValue[0]) params.joblist = this.filterValue[0].join(',')
				if (this.filterValue[1]) params.taglist = this.filterValue[1].join(',')
				if (this.location) {
					params.lng = this.location.longitude
					params.lat = this.location.latitude
				}
				if (this.searchName)
					params.name = this.searchName
				getAllJobs(params)
					.then(res => {
						if (res.length) {
							this.jobList = this.jobList.concat(res)
							this.page++
						}
					})
					.catch(err => {
						uni.showToast({
							title: '加载职位列表失败',
							icon: 'none'
						})
					})
			}
		},
		onPageScroll(res) {
			if (res.scrollTop > this.initialMenuTop) {
				this.menuPosition = 'fixed'
				this.menuTop = 0
			} else {
				this.menuPosition = 'absolute'
				this.menuTop = this.initialMenuTop
			}
		},
		onReachBottom(e) {
			this.loadData()
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.location-box {
			padding: 20rpx;
			background-color: #fff;

			&>text {
				margin-left: 10rpx;
			}

			&>text:nth-of-type(2) {
				color: #e7767c;
			}
		}

		.tag-box {
			margin-bottom: 20rpx;
			background-color: #fff;

			.tag-list {
				display: grid;
				grid-template-columns: repeat(3, 1fr);
				grid-template-rows: repeat(2, 80rpx);
				gap: 10rpx;
				padding: 10rpx 20rpx 20rpx 20rpx;

				.tag-item {
					text-align: center;
					line-height: 80rpx;
					border-radius: 10rpx;
					color: #fff;
					cursor: pointer;
				}
			}
		}

		.search-box {
			height: 44px;
			margin-bottom: 10rpx;
			padding: 0 20rpx;
		}

		.dropdown-box {
			height: 44px;
		}

		.job-box {
			.job-list {
				padding: 20rpx;
				background-color: #f5f5f5;

				.job-item {
					margin-bottom: 20rpx;
					border-radius: 10rpx;
					background-color: #fff;

					&:last-of-type {
						margin-bottom: 0;
					}

					.top {
						position: relative;
						padding: 20rpx 20rpx 0 20rpx;

						&::after {
							content: '';
							position: absolute;
							left: 0;
							width: 100%;
							height: 1px;
							background-color: #e5e5e5;
						}

						.title {
							font-size: 30rpx;
							margin-bottom: 20rpx;

							.distance {
								font-size: 20rpx;
								color: #999999;
								float: right;
								margin-top: 5rpx;
							}
						}

						.tag {
							display: flex;
							overflow: hidden;
							margin-bottom: 20rpx;

							.tag-item {
								font-size: 26rpx;
								margin-left: 10rpx;
								padding: 4rpx 8rpx;
								color: #749ced;
								background-color: #edeffc;

								&:first-of-type {
									margin-left: 0;
								}
							}
						}
					}

					.bottom {
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding: 10rpx 20rpx;

						.price {
							color: #D83C4B;
						}

						.look-btn {
							background-color: #07C160 !important;
							color: #fff;
							padding: 10rpx 20rpx;
							border-radius: 8rpx;
						}
					}
				}
			}
		}
	}
</style>